<style type="text/css">
	.border-container::after {
		content: "Produto";
	}
	
	.sectionTitle {
		font-size: 12px;
		font-weight: bold;
		color: #bbb;
		text-transform: uppercase;
		letter-spacing: 1px;
	}
</style>
<div class="container">
	{{ Form::open(array('url' => 'product', 'class' => 'form-horizontal', 'role' => 'form')) }}
		<div class="border-container">
			@if (!$errors->isEmpty())
			<div class="alert alert-danger">
				<strong>Ocorreram os seguintes erros:</strong>
				{{ HTML::ul($errors->all()) }}
			</div>
			@endif
			
            <div class="form-group">
            	<label for="name" class="col-sm-2 control-label">Nome do Produto </label>
				<div class="col-sm-6">
                	{{ Form::text('name', null, array('class'=>'form-control', 'required'=>'required', 'autocomplete'=>'off')) }}
				</div>
            </div>
            
            <h4><span class="sectionTitle">Composição</span></h4>
            
            <div class="form-group">
            	<div class="col-sm-2 centered">
            		<label>Tipo</label>
            	</div>
            	<div class="col-sm-2 centered">
            		<label>Código do CIGAM</label>
            	</div>
            	<div class="col-sm-1 centered">
            		<label>Qtd</label>
            	</div>
            	<div class="col-sm-4">
            		<label>Nome do Componente</label>
            	</div>
            	<div class="col-sm-3">
            		<label>Ações</label>
            	</div>
            </div>
            
            @if (!empty($inputComponents))
	            @foreach ($inputComponents as $key => $item)
					<div class="form-group component-group">
						<div class="col-sm-2" style="padding-right: 3px;">
							<select name="accessories[]" class="form-control cboOperationType">
								<option value="0"@if ($inputAccessories[$key] == 0) selected@endif>Operação</option>
								<option value="1"@if ($inputAccessories[$key] == 1) selected@endif>Insumo</option>
							</select>
						</div>
						<div class="col-sm-2 col-component">
							<input type="text" class="form-control txtCigamCode" name="cigamCodes[]" value="{{ $inputCigamCodes[$key] }}" autocomplete="off" required="required"@if ($inputAccessories[$key] == 1) readonly="readonly"@endif>
						</div>
						<div class="col-sm-1 col-component">
			    			<input type="text" class="form-control" name="amounts[]" value="{{ $inputAmounts[$key] }}" autocomplete="off" required="required">
			  			</div>
			  			<div class="col-sm-4 col-component">
			    			<input type="text" class="form-control ui-autocomplete-input" name="components[]" value="{{ $item }}" autocomplete="off" required="required">
			  			</div>
			  			<div class="col-sm-3 col-component">
							<input type="hidden" name="costCentres[]" value="{{ $inputCostCentres[$key] }}">
			    			<button type="button" class="btn btn-danger btn-sm btnRemoveComponent" data-toggle="tooltip" data-placement="top" title="Remover Componente">
			      				<span class="glyphicon glyphicon-trash"></span>
			    			</button>
			  			</div>
					</div>
	            @endforeach
			@endif
			
			@if (!empty($productComponents))
				@foreach ($productComponents as $productComponent)
					<div class="form-group component-group current-component">
						<div class="col-sm-2" style="padding-right: 3px;">
							{{ Form::select('accessories[]', array('0'=>'Operação','1'=>'Insumo'),  $productComponent->component->accessory, array('class'=>'form-control cboOperationType')) }}
						</div>
						<div class="col-sm-2 col-component">
							<?php
								$currentCigamCodeAttributes = array('class'=>'form-control txtCigamCode', 'required'=>'required', 'autocomplete'=>'off');
								if ($productComponent->component->accessory == 1) {
									$currentCigamCodeAttributes['readonly']='readonly';
								}
							?>
							{{ Form::text('cigamCodes[]', ($productComponent->component->accessory) ? 'INSUMO' : '', $currentCigamCodeAttributes) }}
						</div>
						<div class="col-sm-1 col-component">
							{{ Form::text('amounts[]', $productComponent->amount, array('class'=>'form-control', 'required'=>'required')) }}
						</div>
			  			<div class="col-sm-4 col-component">
			    			{{ Form::text('components[]', $productComponent->component->name, array('class'=>'form-control', 'required'=>'required')) }}
			  			</div>
			  			<div class="col-sm-3 col-component">
							{{ Form::hidden('costCentres[]', join($productComponent->component->costsCentres()->lists('id'), ',')) }}
			    			<button type="button" class="btn btn-danger btn-sm btnRemoveComponent" data-toggle="tooltip" data-placement="top" title="Remover Componente">
			      				<span class="glyphicon glyphicon-trash"></span>
			    			</button>
			  			</div>
					</div>
				@endforeach
			@endif

			<div class="form-group">
				<div class="col-sm-12">
					<button id="btnAddComponent" type="button" class="btn btn-primary">Adicionar Componente</button>
				</div>
			</div>
      	</div>
      
      	{{ Form::submit('Cadastrar Produto', array('id' => 'btn-save-product', 'class'=>'btn btn-primary', 'data-loading-text' => 'Aguarde...'))}}
		{{ HTML::link('product', 'Cancelar', array('class' => 'btn btn-default', 'style'=>'float: right')) }}
	{{ Form::close() }}
</div>
<script type="text/javascript">
	var allCostsCentres = {{ $costsCentres }};
	$(document).ready(function() {
		$("input[name='costCentres[]']").costsCentres({ costsCentres: allCostsCentres });
		@if (empty($inputComponents) && empty($productComponents))
			$("#btnAddComponent").trigger("click");
			$("input[name='name']").focus();
		@endif
	});
</script>